package com.ssy.lingxi.platform.manage.serviceimpl;

import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.platform.manage.api.model.dto.parameter.ParameterManageUpdateDTO;
import com.ssy.lingxi.platform.manage.api.model.dto.parameter.ParameterManageUpdateStateDTO;
import com.ssy.lingxi.platform.manage.api.model.vo.parameter.ParameterManagePageVO;
import com.ssy.lingxi.platform.manage.api.model.vo.parameter.PlatformParameterManageVO;
import com.ssy.lingxi.platform.manage.entity.base.PlatformParameterManage;
import com.ssy.lingxi.platform.manage.repository.PlatformParameterManageRepository;
import com.ssy.lingxi.platform.manage.service.PlatformParameterManageService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wrc
 * @version 2.0.0
 * @menu 平台参数 - 业务实现层
 * @date 2020/7/20
 */
@Service
public class PlatformParameterManageServiceImpl implements PlatformParameterManageService {

    @Resource
    private PlatformParameterManageRepository platformParameterManageRepository;

    /**
     * 平台参数管理详情查询
     */
    @Override
    public Wrapper<PlatformParameterManageVO> parameterManageDetails(String code) {
        PlatformParameterManage parameterManage = platformParameterManageRepository.findByCode(code);
        if (Objects.isNull(parameterManage)) {
            return Wrapper.fail(ResponseCode.MAN_RECORDS_DON_T_EXIST);
        }

        PlatformParameterManageVO platformParameterManageResponse = new PlatformParameterManageVO();
        platformParameterManageResponse.setId(parameterManage.getId());
        platformParameterManageResponse.setCode(parameterManage.getCode());
        platformParameterManageResponse.setName(parameterManage.getName());
        platformParameterManageResponse.setDetails(parameterManage.getDetails());
        platformParameterManageResponse.setParameterValue(Double.valueOf(parameterManage.getParameterValue()));
        platformParameterManageResponse.setState(parameterManage.getState());
        return Wrapper.success(platformParameterManageResponse);
    }

    /**
     * 参数配置信息分页列表
     *
     * @param request 接口参数
     * @return 返回结果
     */
    @Override
    public Wrapper<PageData<ParameterManagePageVO>> parameterManagePage(PageVO request) {
        Pageable page = PageRequest.of(request.getCurrent() - 1, request.getPageSize(), Sort.by("id").ascending());
        Page<PlatformParameterManage> pageList = platformParameterManageRepository.findAll(page);

        List<ParameterManagePageVO> resultList = pageList.stream().map(e -> {
            ParameterManagePageVO response = new ParameterManagePageVO();
            BeanUtil.copyProperties(e, response);
            return response;
        }).collect(Collectors.toList());

        return Wrapper.success(new PageData<>(pageList.getTotalElements(), resultList));
    }

    /**
     * 参数配置信息修改
     *
     * @param request 接口参数
     * @return 返回结果
     */
    @Override
    public Wrapper<Void> parameterManageUpdate(ParameterManageUpdateDTO request) {
        PlatformParameterManage parameterManage = platformParameterManageRepository.findById(request.getId()).orElse(null);
        if (Objects.isNull(parameterManage)) {
            return Wrapper.fail(ResponseCode.MAN_RECORDS_DON_T_EXIST);
        }

        parameterManage.setParameterValue(request.getParameterValue());
        platformParameterManageRepository.saveAndFlush(parameterManage);

        return Wrapper.success();
    }

    /**
     * 参数配置信息停用/启用
     *
     * @param request 接口参数
     * @return 返回结果
     */
    @Override
    public Wrapper<Void> parameterManageUpdateState(ParameterManageUpdateStateDTO request) {
        PlatformParameterManage parameterManage = platformParameterManageRepository.findById(request.getId()).orElse(null);
        if (Objects.isNull(parameterManage)) {
            return Wrapper.fail(ResponseCode.MAN_RECORDS_DON_T_EXIST);
        }

        parameterManage.setState(request.getState());
        platformParameterManageRepository.saveAndFlush(parameterManage);

        return Wrapper.success();
    }

    /**
     * 获取商家审核评价内容平台参数
     *
     * @return 参数值
     */
    @Override
    public Wrapper<Integer> findMemberEvaluation() {
        //目前写死商家审核评价code为A07
        PlatformParameterManage memberEvaluation = platformParameterManageRepository.findByCode("A07");
        if (memberEvaluation == null) {
            return Wrapper.success();
        }
        return Wrapper.success(Integer.valueOf(memberEvaluation.getParameterValue()));
    }

}
